package com.ming.stringstudy.feedbacksystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.stringstudy.feedbacksystem.entity.RolePermission;
import com.ming.stringstudy.feedbacksystem.mapper.RolePermissionMapper;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.stringstudy.feedbacksystem.vojo.RolePermissionVojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aming
 * @since 2025-01-04
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Integer> selectByRoleId(int roleId) {
        return rolePermissionMapper.selectByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<RolePermissionVojo> rolePermissionVojoList = rolePermissionMapper.findAll();

        PageInfo<RolePermissionVojo> r = new PageInfo<>(rolePermissionVojoList);
        PageBean pageBean = new PageBean(r.getTotal(), r.getList());
        return pageBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addRolePermission(RolePermission rolePermission) {
        if (rolePermission.getRoleId() ==null || rolePermission.getRoleId() == 0 || rolePermission.getPermissionId() ==null || rolePermission.getPermissionId() == 0){
            return 0;
        }
        if (rolePermissionMapper.selectByrpId(rolePermission) != null){
            return 0;
        }
        int result = rolePermissionMapper.insert(rolePermission);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRolePermission(RolePermission rolePermission) {
        if (rolePermission.getId()==null || rolePermission.getId()==0 || rolePermission.getRoleId() ==null || rolePermission.getRoleId() == 0 || rolePermission.getPermissionId() ==null || rolePermission.getPermissionId() == 0){
            return 0;
        }
        if (rolePermissionMapper.selectByrpId(rolePermission) != null){
            return 0;
        }
        int result = rolePermissionMapper.updateById(rolePermission);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRolePermission(List<Integer> ids) {
        int result = rolePermissionMapper.deleteBatchIds(ids);
        return result;
    }
}
